namespace IMPlant.Core.Data.Ado.BaseSupport
{
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using IMPlant.Core.Configuration;
    using IMPlant.Core.Common.Enum;
    using System.Configuration;
    using System.Xml;
    using System.Web;

    public class SqlClientDataBase
    {
        private string connectionStr;
        private static DbProviderFactory dbProviderFactory;

        //public SqlClientDataBase(string dbName) : this(ConfigerHandler.GetConfig(SurportsType.Ado, dbName, KeyType.ConnectionString), SqlClientFactory.Instance)
        //{

        //}
        public SqlClientDataBase(string dbName): this("config", SqlClientFactory.Instance)
        {

        }

        public SqlClientDataBase(string connStr, DbProviderFactory dbpFactory)
        {
            this.connectionStr = connStr;
            dbProviderFactory = dbpFactory;
        }

        public DbCommand CreateCommand(CommandType type, string commText)
        {
            DbCommand command = dbProviderFactory.CreateCommand();
            command.CommandType = type;
            command.CommandText = commText;
            return command;
        }

        public DbConnection CreateConnection()
        {
            DbConnection connection = dbProviderFactory.CreateConnection();
            //connection.ConnectionString = this.connectionStr;

            //XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load(System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, "UnitsXML\\Units.xml"));
            //XmlNode root = xmlDoc.SelectSingleNode("Units");
            //XmlNodeList nodeList = root.ChildNodes;
            //string nd = string.Empty;
            //foreach (XmlNode node in nodeList)
            //{
            //    XmlElement e = ((XmlElement)node);
            //    if (e.GetAttribute("dwdm").ToString().Trim() == SMTC.WebCommon.Common.DWID.ID)
            //        nd = e.GetAttribute("nd").ToString().Trim();
            //}
            connection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            return connection;
        }
        public static SqlClientDataBase CreateDateBase(string dbName)
        {
            return new SqlClientDataBase(dbName);
        }

        public static DbParameter CreateParameter()
        {
            return dbProviderFactory.CreateParameter();
        }

        public DbTransaction CreateTransaction()
        {
            return this.GetOpenConncetion().BeginTransaction();
        }

        public DbTransaction CreateTransaction(DbConnection conn)
        {
            return conn.BeginTransaction();
        }

        public DbTransaction CreateTransactionSerializable()
        {
            return this.GetOpenConncetion().BeginTransaction(IsolationLevel.Serializable);
        }

        public DbTransaction CreateTransactionSerializable(DbConnection conn)
        {
            return this.GetOpenConncetion().BeginTransaction(IsolationLevel.Serializable);
        }

        public DbConnection GetOpenConncetion()
        {
            DbConnection connection = null;
            try
            {
                connection = this.CreateConnection();
                connection.Open();
            }
            catch (Exception exception)
            {
                connection.Dispose();
                throw exception;
            }
            return connection;
        }

        public string GetConnectString
        {
            get
            {
                return this.connectionStr;
            }
        }

        public static DbProviderFactory ProviderFactory
        {
            get
            {
                if (dbProviderFactory == null)
                {
                    dbProviderFactory = SqlClientFactory.Instance;
                }
                return dbProviderFactory;
            }
        }
    }
}

